﻿using DevExpress.XtraEditors;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmFunctionPointEmp : FrmBase
    {
        public FrmFunctionPointEmp()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            for (int i = 0; i < this.gvFrom.RowCount; i++)
            {
                if (this.gvFrom.IsRowSelected(i))
                {
                    DataRow row = this.dtFrom.Rows.Find(this.gvFrom.GetDataRow(i)["EMP_ID"]);
                    DataRow row2 = this.dtTo.NewRow();
                    for (int k = 0; k < this.dtFrom.Columns.Count; k++)
                    {
                        string columnName = this.dtFrom.Columns[k].ColumnName;
                        if (this.dtTo.Columns.Contains(columnName))
                        {
                            row2[columnName] = row[columnName];
                        }
                    }
                    row2["PROPERTY"] = "H";
                    this.dtTo.Rows.Add(row2);
                    list.Add(row);
                }
            }
            for (int j = 0; j < list.Count; j++)
            {
                ((DataRow)list[j]).Delete();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            for (int i = 0; i < this.gvTo.RowCount; i++)
            {
                if (this.gvTo.IsRowSelected(i))
                {
                    DataRow row = this.dtTo.Rows.Find(this.gvTo.GetDataRow(i)["EMP_ID"]);
                    DataRow row2 = this.dtFrom.NewRow();
                    for (int k = 0; k < this.dtTo.Columns.Count; k++)
                    {
                        string columnName = this.dtTo.Columns[k].ColumnName;
                        if (this.dtFrom.Columns.Contains(columnName))
                        {
                            row2[columnName] = row[columnName];
                        }
                    }
                    this.dtFrom.Rows.Add(row2);
                    list.Add(row);
                }
            }
            for (int j = 0; j < list.Count; j++)
            {
                ((DataRow)list[j]).Delete();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.dtTo.AcceptChanges();
            string[] commandtexts = new string[this.dtTo.Rows.Count + 1];
            DataParamCol[] parms = new DataParamCol[this.dtTo.Rows.Count + 1];
            commandtexts[0] = "DELETE FROM MXHLY_SYS_FUNCTION_POINT_EMP WHERE FUNCTION_POINT_CODE = :function_point_code";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("function_point_code", this.functionPointCode) });
            for (int i = 0; i < this.dtTo.Rows.Count; i++)
            {
                commandtexts[i + 1] = "INSERT INTO MXHLY_SYS_FUNCTION_POINT_EMP (FUNCTION_POINT_CODE ,EMP_ID ,PROPERTY) \r\n    VALUES (:fcode ,:emp_id ,:property)";
                parms[i + 1] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", this.functionPointCode), new InputDataParam("emp_id", this.dtTo.Rows[i]["EMP_ID"]), new InputDataParam("property", this.dtTo.Rows[i]["PROPERTY"].ToString()) });
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                this.dtTo_s.Rows.Clear();
                for (int j = 0; j < this.dtTo.Rows.Count; j++)
                {
                    if (this.dtTo.Rows[j].RowState != DataRowState.Deleted)
                    {
                        this.dtTo_s.Rows.Add(this.dtTo.Rows[j].ItemArray);
                    }
                }
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
                base.Close();
            }
            else
            {
                XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage);
            }
        }

        public bool SetParaMeter(DataTable dtAll, DataTable dt_choice, string function_point_code)
        {
            if (!base.Init())
            {
                return false;
            }
            this.functionPointCode = function_point_code;
            this.dtFrom = dtAll.Copy();
            this.dtTo = dt_choice.Copy();
            this.dtTo_s = dt_choice;
            for (int i = 0; i < this.dtTo.Rows.Count; i++)
            {
                DataRow row = this.dtFrom.Rows.Find(this.dtTo.Rows[i]["EMP_ID"]);
                if (row != null)
                {
                    this.dtFrom.Rows.Remove(row);
                }
            }
            DataTable table = new DataTable();
            table.Columns.Add("PROPERTY_CODE", typeof(string));
            table.Columns.Add("PROPERTY_NAME", typeof(string));
            table.Rows.Add(new object[] { "H", "不可视" });
            table.Rows.Add(new object[] { "R", "只读" });
            table.Rows.Add(new object[] { "W", "全部" });
            this.riProperty.ValueMember = "PROPERTY_CODE";
            this.riProperty.DisplayMember = "PROPERTY_NAME";
            this.riProperty.DataSource = table;
            this.gridControl1.DataSource = this.dtFrom;
            this.gridControl2.DataSource = this.dtTo;
            return true;
        }
    }
}
